<template>
  <div>
    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="日期">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="address" label="地址" show-overflow-tooltip> </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="downFile" type="success" size="mini">导出Excel</el-button>
    </div>
  </div>
</template>

<script>
import xlxs from 'xlsx'
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-08",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-06",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-07",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
      multipleSelection: [], // 选中的数组
    }
  },

  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    downFile(){
      let arr = this.tableData.map(item => {
        return {
          '姓名': item.name,
          '地址': item.address,
          '日期': item.date
        }
      })
      // 将数据转换为表格
      let sheet = xlxs.utils.json_to_sheet(arr)
      let book = xlxs.utils.book_new()
      xlxs.utils.book_append_sheet(book, sheet, "sheet1")
      xlxs.writeFile(book, `user${new Date().getTime()}.xls`)
    }
  },
}
</script>
